/**
* googTab.js  Tab分页效果操作封装
* dev 版本 v 1.0.1.220316_Alpha
* 更新时间 2022-03-16
* 作者: Goog、2022
*/
function GoogTab(_edit) {
this.edit = _edit; // 配置参数
this.getEl = function (matchName, startNode = document) { // 获取节点
    if(startNode.querySelectorAll(matchName).length === 0 ){return document} // 没找到就返回document
    return startNode.querySelectorAll(matchName).length === 1 ? startNode.querySelector(matchName) : startNode.querySelectorAll(matchName);
}
this.loopEvents = function (_callback, arg) { // 循环处理节点或节点数组
    let index = 0
    arg && arg.forEach(item => {
        if (item instanceof NodeList || item instanceof HTMLCollection) {
            item.forEach(itemChild => {
                _callback && _callback(itemChild, index, item);
            })
        } else {
            _callback && _callback(item, index, arg);
        }
        index++;
    })
}
this.setStyle = function (styleObj, ...arg) { // 给一个节点或者多个节点,或者节点数组设置一堆样式
    this.loopEvents((_item) => {
        for (styleName in styleObj) {
            _item.style[styleName] = styleObj[styleName];
        }
    }, arg)
}
this.addClass = function (className, ...arg) { // 给一个节点或者多个节点添加样式
    this.loopEvents((_item) => {
        _item.classList.add(className);
    }, arg)
}
this.removeClass = function (className, ...arg) { // 给一个节点或者多个节点删除样式
    this.loopEvents((_item) => {
        _item.classList.remove(className);
    }, arg)
}
this.hideEl = function (...arg) { // 让一个或者多个元素隐藏
    this.loopEvents((_item) => {
        _item.style.display = 'none';
    }, arg)
}
this.showEl = function (...arg) { // 让一个或者多个元素显示
    this.loopEvents((_item) => {
        _item.style.display = 'block';
    }, arg)
}
this.setVal = function (_value,...arg){
  this.loopEvents((_item) => {
    if(typeof _item.value === 'undefined'){
      _item.value = _value;
    }else {
      _item.innerHTML = _value;
    }
}, arg)
}
this.init = function (_callback,_defaultCallback) { // 初始化 默认选中的序号 第二个参数是默认调用函数
    var that = this; // 锁定this指向
    /* ----------------------- 配置参数 开始 */
    var { el, btnEl, btnChildEl, contEl, contChildEl, eventName } = this.edit
    var $eventName = eventName || 'click';
    var $el = el || 'body'; // 找不到就去找body
    var $root = this.getEl($el);
    var $btnEl = this.getEl(btnEl, $root) || this.getEl($el).firstElementChild;
    var $btnChildEl = this.getEl(btnChildEl, $btnEl) || this.getEl(btnEl, $root).children;
    var $contEl = this.getEl(contEl, $root) || this.getEl($el).lastElementChild;
    var $contChildEl = this.getEl(contChildEl, $contEl) || this.getEl(contEl, $root).children;
    this.$edit = { $root, $btnEl, $btnChildEl, $contEl, $contChildEl, $eventName }
    /*------------------------- 配置参数 结束 */
    /* 1. 隐藏所有的内容框 */
    this.hideEl($contChildEl);
    /* 2. 给标签按钮添加事件 */
    for (let i = 0; i < $btnChildEl.length; i++) {
        $btnChildEl[i].addEventListener($eventName, function () {
            _callback && _callback($btnChildEl[i],$contChildEl[i],i,$btnChildEl,$contChildEl);
        });
    }
    /* 3.默认调用函数 */
    _defaultCallback&&_defaultCallback($btnChildEl,$contChildEl); 
};
}